from django.http import FileResponse, HttpResponse
from django.shortcuts import redirect
from django.utils.encoding import escape_uri_path

from myapp.models import Update


def downloadapp(request):
    file=open('/root/Git/supernews/static/upload/topnews.apk','rb')
    response =HttpResponse(file)
    response['Content-Type']='application/octet-stream'
    version = Update.objects.filter(type='latest')[0].version
    response['Content-Disposition']= "attachment;filename*=utf-8''{}".format(escape_uri_path('超级头条'+version+'.apk'))
    return response
def downloadapps(request):
    file=open('/root/Git/supernews/static/upload/topnewss.apk','rb')
    response =HttpResponse(file)
    response['Content-Type']='application/octet-stream'
    version = Update.objects.filter(type='latest')[0].version
    response['Content-Disposition']= "attachment;filename*=utf-8''{}".format(escape_uri_path('超级头条'+version+'.apk'))
    return response
def downloadrich(request):
    file=open('/root/Git/supernews/static/upload/rich.apk','rb')
    response =HttpResponse(file)
    response['Content-Type']='application/octet-stream'
    version = Update.objects.filter(type='latest')[0].version
    response['Content-Disposition']= "attachment;filename*=utf-8''{}".format(escape_uri_path('聚鑫头条'+version+'.apk'))
    return response
def version(request):
    if request.POST:
        version = request.POST['version']
        detial = request.POST['detial']
        if request.POST['force']=='true':
            force=True
        else:
            force=False
        list = Update.objects.filter(type='latest')
        i=list[0]
        i.version=version
        i.force=force
        i.detial=detial
        i.save()
    return redirect('/sys')